package org.wlz;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.*;

public class ClubManagementSystem {
    private static final String EXCEL_FILE_PATH = "C:\\Users\\20833\\Desktop\\java.xlsx";
    private static final String TXT_FILE_PATH = "C:\\Users\\20833\\Desktop\\club_members.txt";

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ClubManager clubManager = new ClubManager();

        try {
            clubManager.loadClubsFromExcel(EXCEL_FILE_PATH);
            System.out.println("社团数据已从 Excel 文件加载！");
        } catch (IOException e) {
            System.out.println("加载 Excel 文件时发生错误: " + e.getMessage());
        }

        while (true) {
            System.out.println("1. 查看所有社团信息");
            System.out.println("2. 查看特定社团信息");
            System.out.println("3. 转移成员部门");
            System.out.println("4. 导入 Excel 活动记录并导出为 TXT");
            System.out.println("5. 退出");
            System.out.print("选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine();

            if (choice == 1) {
                clubManager.printAllClubsInfo();
            } else if (choice == 2) {
                System.out.print("请输入社团名称: ");
                String clubName = scanner.nextLine();
                clubManager.printClubInfo(clubName);
            } else if (choice == 3) {
                System.out.print("请输入成员学号: ");
                int studentID = scanner.nextInt();
                scanner.nextLine();
                System.out.print("请输入目标部门: ");
                String newDepartmentName = scanner.nextLine();

                Member member = null;
                // 查找成员对象，并确保它在 Club 对象内
                for (Club club : clubManager.getAllClubs()) {
                    for (Member m : club.getMembers()) {
                        if (m.getStudentID() == studentID) {
                            member = m;
                            break;
                        }
                    }
                    if (member != null) break;
                }

                if (member != null) {
                    Department newDepartment = null;
                    // 确保我们在正确的 Club 对象内查找部门
                    for (Department dept : member.getClub().getDepartments()) {
                        if (dept.getName().equals(newDepartmentName)) {
                            newDepartment = dept;
                            break;
                        }
                    }
                    if (newDepartment != null) {
                        clubManager.transferMemberToDepartment(member, newDepartment);
                    } else {
                        System.out.println("未找到目标部门!");
                    }
                } else {
                    System.out.println("未找到该成员!");
                }
            } else if (choice == 4) {
                try {
                    clubManager.exportActivityToTxt(TXT_FILE_PATH);
                    System.out.println("活动记录已导出到 TXT 文件!");
                } catch (IOException e) {
                    System.out.println("导出失败: " + e.getMessage());
                }
            } else if (choice == 5) {
                break;
            }
        }
        scanner.close();
    }

    // ClubManager 类
    static class ClubManager {
        private Map<String, Club> clubs;

        public ClubManager() {
            this.clubs = new HashMap<>();
        }

        public void addClub(Club club) {
            clubs.put(club.getName(), club);
        }

        public Club getClub(String clubName) {
            return clubs.get(clubName);
        }

        public void transferMemberToDepartment(Member member, Department department) {
            department.addMember(member);
            member.setDepartment(department.getName());
        }

        public void removeMemberFromClub(Member member) {
            Club club = getClub(member.getClub().getName());
            if (club != null) {
                club.removeMember(member);
            }
        }

        public void printClubInfo(String clubName) {
            Club club = getClub(clubName);
            if (club != null) {
                System.out.println("社团名称: " + club.getName());
                for (Department dept : club.getDepartments()) {
                    System.out.println("  部门: " + dept.getName());
                    for (Member member : dept.getMembers()) {
                        System.out.println("    成员: " + member.getName() + " (学号: " + member.getStudentID() + ")");
                    }
                }
            } else {
                System.out.println("未找到该社团!");
            }
        }

        public Collection<Club> getAllClubs() {
            return clubs.values();
        }

        public void printAllClubsInfo() {
            for (Club club : getAllClubs()) {
                printClubInfo(club.getName());
                System.out.println();
            }
        }

        // 从 Excel 文件中加载社团、部门和成员信息
        public void loadClubsFromExcel(String excelFilePath) throws IOException {
            try (FileInputStream fileIn = new FileInputStream(excelFilePath);
                 Workbook workbook = new XSSFWorkbook(fileIn)) {
                Sheet sheet = workbook.getSheetAt(0);

                for (Row row : sheet) {
                    String clubName = row.getCell(0).getStringCellValue();
                    String deptName = row.getCell(1).getStringCellValue();
                    String memberName = row.getCell(2).getStringCellValue();
                    int studentID = 0;

                    // 检查单元格类型，确保获取数字
                    Cell cell = row.getCell(3);
                    if (cell.getCellType() == CellType.NUMERIC) {
                        studentID = (int) cell.getNumericCellValue();
                    } else if (cell.getCellType() == CellType.STRING) {
                        try {
                            studentID = Integer.parseInt(cell.getStringCellValue().trim()); // 去掉前后空格
                        } catch (NumberFormatException e) {
                            System.out.println("错误：学号应该是数字，但在 Excel 文件中找到了字符串！学号: " + row.getCell(3).getStringCellValue());
                            continue; // 跳过当前行
                        }
                    }

                    Club club = clubs.get(clubName);
                    if (club == null) {
                        club = new Club(clubName);
                        addClub(club);
                    }

                    Department department = null;
                    for (Department dept : club.getDepartments()) {
                        if (dept.getName().equals(deptName)) {
                            department = dept;
                            break;
                        }
                    }

                    if (department == null) {
                        department = new Department(deptName);
                        club.addDepartment(department);
                    }

                    Member member = new Member(memberName, studentID, deptName, club);
                    department.addMember(member);
                    club.addMember(member);
                }
            }
        }

        // 将活动记录导出到 TXT 文件
        public void exportActivityToTxt(String txtFilePath) throws IOException {
            try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(txtFilePath), "UTF-8"))) {
                for (Club club : getAllClubs()) {
                    for (Department department : club.getDepartments()) {
                        writer.write("社团: " + club.getName());
                        writer.newLine();
                        writer.write("部门: " + department.getName());
                        writer.newLine();
                        writer.write("成员列表:");
                        writer.newLine();
                        for (Member member : department.getMembers()) {
                            writer.write("  " + member.getName() + " (学号: " + member.getStudentID() + ")");
                            writer.newLine();
                        }
                        writer.newLine();
                    }
                }
            }
        }
    }

    // Club 类
    static class Club {
        private String name;
        private List<Department> departments;
        private List<Member> members;

        public Club(String name) {
            this.name = name;
            this.departments = new ArrayList<>();
            this.members = new ArrayList<>();
        }

        public String getName() {
            return name;
        }

        public void addDepartment(Department department) {
            departments.add(department);
        }

        public void addMember(Member member) {
            members.add(member);
        }

        public void removeMember(Member member) {
            members.remove(member);
        }

        public List<Department> getDepartments() {
            return departments;
        }

        public List<Member> getMembers() {
            return members;
        }
    }

    // Department 类
    static class Department {
        private String name;
        private List<Member> members;

        public Department(String name) {
            this.name = name;
            this.members = new ArrayList<>();
        }

        public String getName() {
            return name;
        }

        public void addMember(Member member) {
            members.add(member);
        }

        public List<Member> getMembers() {
            return members;
        }
    }

    // Member 类
    static class Member {
        private String name;
        private int studentID;
        private String department;
        private Club club;

        public Member(String name, int studentID, String department, Club club) {
            this.name = name;
            this.studentID = studentID;
            this.department = department;
            this.club = club;
        }

        public String getName() {
            return name;
        }

        public int getStudentID() {
            return studentID;
        }

        public String getDepartment() {
            return department;
        }

        public Club getClub() {
            return club;
        }

        public void setDepartment(String department) {
            this.department = department;
        }
    }
}
